315. Network
A
Telephone Line Company (TLC) is establishing a new telephone cable network.
They are connecting several places numbered by integers from 1 to n. No
two places have the same number. The lines are bidirectional and always connect
together two places and in each place the lines end in a telephone exchange.
There is one telephone exchange in each place. From each place it is possible
to reach through lines every other place, however it need not be a direct
connection, it can go through several exchanges. From time to time the power
supply fails at a place and then the exchange does not operate.
The officials from TLC realized that in such a case it can
happen that besides the fact that the place with the failure is unreachable,
this can also cause that some other places cannot connect to each other. In
such a case we will say the place (where the failure occurred) is critical. Now
the officials are trying to write a program for finding the number of all such
critical places. Help them.
Input. The input consists of several blocks of lines. Each
block describes one network. In the first line of each block there is the
number of places n < 100. Each of the next at most n lines
contains the number of a place followed by the numbers of some places to which
there is a direct line from this place. These at most n lines completely
describe the network, i.e., each direct connection of two places in the network
is contained at least in one row. All numbers in one line are separated by one
space. Each block ends with a line containing just ‘0’. The last block has only
one line with n = 0.
Output. The output contains for each block except the last in
the input file one line containing the number of critical places.
5
5 1 2 3 4
0
6
2 1 3
5
4 6 2
0
0
1
2
ãðàôû – òî÷êè ñî÷ëåíåíèÿ
 çàäà÷å ñëåäóåò íàéòè
êîëè÷åñòâî òî÷åê ñî÷ëåíåíèÿ.
#include <stdio.h>
#include <string.h>
#define MAX 110
int g[MAX][MAX], used[MAX], d[MAX],
up[MAX], ArtPoint[MAX];
int i, n, a, b, time, res;
char ch;
int min(int
i, int j)
{
return (i < j) ? i : j;
}
void dfs (int
v, int p = -1)
{
int to, children;
used[v] = 1;
d[v] = up[v] =
time++;
children = 0;
for (to = 1; to <= n; to++)
{
if(!g[v][to]) continue;
if (to == p) continue;
if (used[to])
up[v] = min
(up[v], d[to]);
else
{
dfs (to, v);
up[v] =
min(up[v], up[to]);
if ((up[to] >= d[v]) && (p != -1))
ArtPoint[v] = 1;
++children;
}
}
if ((p == -1) && (children > 1))
ArtPoint[v] = 1;
}
int main(void)
{
while(scanf("%d",&n),n)
{
memset(g,0,sizeof(g));
memset(used,0,sizeof(used));
memset(up,0,sizeof(up));memset(d,0,sizeof(d));
memset(ArtPoint,0,sizeof(ArtPoint));
while(scanf("%d
",&a),a)
{
do
{
scanf("%d%c",&b,&ch);
g[a][b] =
g[b][a] = 1;
} while(ch != '\n');
}
time = 1; res = 0;
for(i = 1; i <= n; i++)
if (!used[i]) dfs(i);
for(i = 1; i <= n; i++) res += ArtPoint[i];
printf("%d\n",res);
}
return 0;
}